<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class Feedback
{
    public function __construct($id, $name, $feed)
    {
        $this->id = $id;
        $this->name = $name;
        $this->feed = $feed;
    }
    protected function script()
    {
        return <<<SCRIPT

    layui.use('layer', function(){
        var layer = layui.layer;
        $('.follow').click(function () {
           var that = this;
           var id = $(that).attr('data-id');
           var name = $(that).attr('data-name');
           var feed = $(that).attr('data-feed');
           var index = layer.open({
               title: '<\span style="color:red;">'+name+'</\span>',
               type: 2,
               content: '/admin/common/feedback/recall?id='+id,
               area:['300px', '200px'],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var content = $(layero).find("iframe").contents().find("input[name=content]").val();
                    $.ajax({
                        url: '/admin/common/feedback/recall',
                        type: 'post',
                        data: {
                            content: content,
                            id: id,
                        },
                        success: function (res) {
                            if(res.code) {
                                layer.msg(res.text)
                                return;
                            }
                            layer.msg(res.text)
                            layer.close(index);
                            location.href = location.href
                        }
                    })
                }
           });
       });
    });
SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return "&nbsp;<a href='javascript:void(0);'><button data-feed='{$this->feed}'  data-id='{$this->id}' data-name='{$this->name}' class='follow layui-btn layui-bg-green layui-btn-xs'>回复</button></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}